package com.csjung.games.gamenori.client.desktop;

import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.DeckLayoutPanel;
import com.google.gwt.user.client.ui.HasOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.web.bindery.event.shared.EventBus;

import com.csjung.games.gamenori.client.GamenoriShell;
import com.csjung.games.gamenori.client.place.MatchingPlace;
import com.csjung.games.gamenori.client.place.WelcomePlace;
import com.csjung.games.gamenori.client.presenter.UserListPresenter;
import com.csjung.games.gamenori.client.ui.DestopUserList;
import com.csjung.games.gamenori.shared.GamenoriRequestFactory;

import java.util.ArrayList;
import java.util.List;

public class GamenoriShellDesktop extends ResizeComposite implements GamenoriShell {

  /**
   * CSS override used for the main menu.
   */
  interface MainMenuStyle extends CellList.Style {
  }

  interface GamenoriShellDesktopUiBinder extends UiBinder<Widget, GamenoriShellDesktop> {
  }

  /**
   * A ClientBundle that provides images for this widget.
   */
  interface Resources extends CellList.Resources {
    /**
     * The styles used in the main menu. We extend {@link CellList.Style#DEFAULT_CSS} with the
     * styles defined in MainMenuCellList.css.
     */
    @Source({"MainMenuCellList.css", CellList.Style.DEFAULT_CSS})
    MainMenuStyle cellListStyle();
  }

  private static GamenoriShellDesktopUiBinder uiBinder = GWT
      .create(GamenoriShellDesktopUiBinder.class);

  @UiField(provided = true)
  LoginWidget loginWidget;

  @UiField
  HasOneWidget destopUserListContainer;

  /**
   * The main menu list.
   */
  @UiField(provided = true)
  CellList<MainMenuItem> mainMenu;

  /**
   * The container that holds content.
   */
  @UiField
  DeckLayoutPanel contentContainer;

  public GamenoriShellDesktop(final GamenoriRequestFactory requestFactory, final EventBus eventBus,
      final PlaceController placeController) {

    loginWidget = new LoginWidget(requestFactory, eventBus);

    // Initialize the main menu.
    Resources resources = GWT.create(Resources.class);
    mainMenu = new CellList<MainMenuItem>(new MainMenuItem.Cell(), resources);
    mainMenu.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

    // We don't expect to have more than 30 menu items.
    mainMenu.setVisibleRange(0, 30);

    // Add items to the main menu.
    final List<MainMenuItem> menuItems = new ArrayList<MainMenuItem>();
    menuItems.add(new MainMenuItem("Welcome", new WelcomePlace()));
    menuItems.add(new MainMenuItem("Matching Game", new MatchingPlace()));

    mainMenu.setRowData(menuItems);

    // Choose a place when a menu item is selected.
    final SingleSelectionModel<MainMenuItem> selectionModel =
        new SingleSelectionModel<MainMenuItem>();
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      public void onSelectionChange(SelectionChangeEvent event) {
        MainMenuItem selected = selectionModel.getSelectedObject();
        if (selected != null && !selected.mapsToPlace(placeController.getWhere())) {
          placeController.goTo(selected.getPlace());
        }
      }
    });
    mainMenu.setSelectionModel(selectionModel);

    // Update selection based on the current place.
    eventBus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() {
      public void onPlaceChange(PlaceChangeEvent event) {
        Place place = event.getNewPlace();
        for (MainMenuItem menuItem : menuItems) {
          if (menuItem.mapsToPlace(place)) {
            // We found a match in the main menu.
            selectionModel.setSelected(menuItem, true);
            return;
          }
        }

        // We didn't find a match in the main menu.
        selectionModel.setSelected(null, true);
      }
    });

    // Initialize this widget.
    initWidget(uiBinder.createAndBindUi(this));
    
    DestopUserList destopUserList = new DestopUserList(requestFactory);
    UserListPresenter userListPresenter = new UserListPresenter(destopUserList, requestFactory);
    userListPresenter.start(eventBus);
    destopUserListContainer.setWidget(userListPresenter);

    /*
     * loginWidget.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { }
     * });
     */
  }

  @Override
  public void setWidget(IsWidget content) {
    contentContainer.setWidget(content);

  }

}
